#include "MemInfoDialog.h"
#include "ui_MemInfoDialog.h"

#include "../JWWS/INCLUDE/SYSINFO.HPP"
#include "../JWTS/INCLUDE/AnsiConverter.h"

#include <QFile>
#include <QFontMetrics>
#include <algorithm>

MemInfoDialog::MemInfoDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::MemInfoDialog)
{
    ui->setupUi(this);

    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);

    QString text;
    int K = 64;
#ifdef Q_OS_WIN
    K = 71;
    text = makeMemInfo();
#else    
    QFile file("/proc/meminfo");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        K = 84;
        text =toQtStr(file.readAll());
    }
#endif
    ui->textEdit->setText(text);
    const auto lines = text.split(QLatin1Char('\n'), Qt::SplitBehaviorFlags::SkipEmptyParts);

    const QFontMetrics fontMetrics(ui->textEdit->font());
    int maxWidth = ui->buttonBox->width();
    for (const auto &line : lines)
    {
        maxWidth = (std::max)(maxWidth,fontMetrics.horizontalAdvance(line));
    }
    setFixedWidth(maxWidth*K/64);
}

MemInfoDialog::~MemInfoDialog()
{
    delete ui;
}
